﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddViewPage.aspx.cs" Inherits="Easy.Web.Pages.EntityPages.AddViewPage" %>

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <link href="/_css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="/_css/common.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="/_css/iconfont.css" />
    <script src="/_js/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="/_js/bootstrap.min.js" type="text/javascript"></script>
    <script src="/_js/json2.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="/_css/bootstrap-select.min.css" />
    <script src="/_js/bootstrap-select.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function addViewMap(type) {
            if ($("#Name").val() == "" || $("#Name").val() == null) {
                alert("视图英文名不能为空！");
                return;
            }
            if ($("#DisplayName").val() == "" || $("#DisplayName").val() == null) {
                alert("视图中文名不能为空！");
                return;
            }

            var viewAttvalue = "";
            var orderby = "";
            $("#viewAtt_table tr[id]").each(function () {
                var attname = $(this).find("td").eq(0).children().val();
                if ($(this).find("td").eq(1).children().val() != "") {
                    order = $(this).find("td").eq(1).children().val();
                }
                else {
                    alert("排序不能为空！");
                }
                var width = $(this).find("td").eq(2).children().val();
                var viewOrderBy = $(this).find("td").eq(3).children().val();
                if (viewOrderBy != "" && viewOrderBy != null) {
                    orderby = viewOrderBy;
                }
                //kyzhao 增加参数：字段来自对应实体

                var entity = $(this).find("td").eq(0).children().eq(0).find("option:selected").attr("entity");
                var linkentity = $(this).find("td").eq(0).children().eq(0).find("option:selected").attr("linkentity");
                var linkattribute = $(this).find("td").eq(0).children().eq(0).find("option:selected").attr("linkattribute");
                viewAttvalue += attname + "α" + order + "α" + width + "α" + viewOrderBy + "α" + entity + "α" + linkentity + "α" + linkattribute + "β";
                //  viewAttvalue += attname + "α" + order + "α" + width + "α" + viewOrderBy + "β";

            });
            if (orderby == "") {
                alert("视图默认排序不能为空！");
                return;
            }
            var JsIdList = [];
            $('#JsList').children('option:selected').each(function() {
                JsIdList.push($(this).val());
            });
            var View = {};
            View["ViewFilter"] = $("#ViewFilter").val();
            View["EntityId"] = $("#EntityId").val();
            View["Name"] = $("#prefix").html() + $("#Name").val();
            View["DisplayName"] = $("#DisplayName").val();
            View["viewAttvalue"] = viewAttvalue;
            View["ViewAdditionalJs"] = "";// $('#ViewAdditionalJs').val();
            View["ViewIsDisplay"] = $("#ViewIsDisplay").val();
            View["ViewAttProcedure"] = $("#ViewAttProcedure").val();
            View["ViewClientType"] = $("#ViewClientType").val();    //增加视图类型
            View["ViewTemplete"] = escape($("#ViewTemplete").val());    //增加Templete
            $.ajax({
                url: "/Service/Entity/AddViewMap.ashx",
                async: false,
                type: "post",
                dataType: "JSON",
                data: {
                    "view": JSON.stringify(View),
                    "jslist": JSON.stringify(JsIdList),
                    "EntityName": $("#EntityName").val()
                },
                success: function (data, textStatus) {
                    if (data.Result == "0") {
                        if (type == 1) {

                            window.location = "AttributeListPage.aspx?Id=" + $("#EntityId").val();

                        }
                        else if (type == 2) {
                            location.reload();
                        }
                    }
                    else {
                        alert(data.Remark);
                    }

                },
                error: function (data, textStatus) {
                    alert("错误");

                }

            });
        }
        function btn_return() {

            window.location = "AttributeListPage.aspx?Id=" + $("#EntityId").val();

        }

        function btnAddRow() {

            var rownum = $("#viewAtt_table tr").length - 1;

            var value = "<select class='form-control' id='att_" + rownum + "' name='value_" + rownum + "' width='90%'>" + $("#options").val() + "</select>";

            var order = "<input class='form-control' type='text' id='order_" + rownum + "' name='order_" + rownum + "' width='50px'/>";
            var width = "<input class='form-control' type='text' id='width_" + rownum + "' name='width_" + rownum + "' width='50px'/>";
            var viewOrderBy = "<input class='form-control' type='text' id='viewOrderBy_" + rownum + "' name='viewOrderBy_" + rownum + "' width='50px'/>";
            var button = "<button type='button' class='btn btn-primary ng-binding' onclick='DelRow(" + rownum + ");'>删除</button>";
            var row = "<tr id='" + rownum + "' ><td>" + value + "</td><td>" + order + "</td><td>" + width + "</td><td>" + viewOrderBy + "</td><td>" + button + "</td></tr>";
            $(row).insertAfter($("#viewAtt_table tr:eq(" + rownum + ")"));
        }

        function DelRow(rownum) {

            $("#viewAtt_table tr[id=" + rownum + "]").remove();
        }

        $(document).ready(function () {




            if ($("#edit").val() == "0") {
                //新建
                $("#page_title").html("新建视图");
                $("[noModify = noModify]").removeAttr("disabled");
                $("#add_btn_div").show();
                $("#update_btn_div").hide();
                GetJsList();
            }
            else if ($("#edit").val() == "1") {

                //修改
                getViewMapInfo();
                $("#page_title").html("修改视图");
                $("[noModify = noModify]").attr('disabled', 'true');
                $("#add_btn_div").hide();
                $("#update_btn_div").show();
            }
            else {
                alert("edit状态错误");
                window.location = "AttributeListPage.aspx?Id=" + $("#EntityId").val();
            }


        });

        function GetJsList() {
            $.ajax({
                url: "/Service/Data/JsHub.ashx",
                type: "post",
                dataType: "json",
                data: {
                    "action": "getjslist"
                },
                success: function (data, textStatus) {
                    var lis = $('#JsList');
                    lis.children().remove();
                    for (var k in data) {
                        var temp = document.createElement('option');
                        temp.innerText = data[k].JsName;
                        temp.value = data[k].JsListId;
                        lis[0].appendChild(temp);
                    }
                    lis.addClass('selectpicker');
                    lis.selectpicker();
                    $("#DisplayName").val(data.DisplayName);
                    if (data.Name.indexOf($("#prefix").html()) == -1) {
                        $("#prefix").html("");
                        $("#Name").val(data.Name);
                    }
                },
                error: function (data, textStatus) {

                }
            })
        }

        function getViewMapInfo() {
            $.ajax({
                url: "/Service/Entity/GetViewMapInfo.ashx",
                async: false,
                type: "post",
                dataType: "json",
                data: {
                    "EntityName": $("#EntityName").val(),
                    "ViewMapName": $("#ViewMapName").val()
                },
                success: function (data, textStatus) {
                    var lis = $('#JsList');
                    lis.children().remove();
                    for (var k in data.ViewJsList) {
                        var temp = document.createElement('option');
                        temp.innerText = data.ViewJsList[k].JsName;
                        temp.value = data.ViewJsList[k].JsListId;
                        if (data.ViewJsList[k].JsCount > 0) {
                            temp.selected = true;
                        }
                        lis[0].appendChild(temp);
                    }
                    lis.addClass('selectpicker');
                    lis.selectpicker();
                    $("#DisplayName").val(data.DisplayName);
                    if (data.Name.indexOf($("#prefix").html()) == -1) {
                        $("#prefix").html("");
                        $("#Name").val(data.Name);
                    }
                    else {
                        $("#Name").val(data.Name.substring(data.Name.indexOf('_') + 1, data.Name.length));
                    }
                    $("#ViewFilter").val(data.ViewFilter);
                    //$("#ViewAdditionalJs").val(data.ViewAdditionalJs);
                    $("#ViewTemplete").val(data.ViewTemplete);
                    if (data.ViewIsDisplay == null || data.ViewIsDisplay == "") {
                        $("#ViewIsDisplay").val("0");
                    }
                    else {
                        var show = data.ViewIsDisplay.toLowerCase() == "true" ? "1" : "0";
                        $("#ViewIsDisplay").val(show);

                    }
                    if (data.ViewAttProcedure != null && data.ViewAttProcedure != "") {
                        $("#ViewAttProcedure").val(data.ViewAttProcedure);
                        getWords();
                    }
                    if (data.ViewClientType != null && data.ViewAttProcedure != "") {
                        $("#ViewClientType").val(data.ViewClientType);
                    }
                    var arr1 = data.viewAttvalue.split('β');
                    for (var i = 0; i < arr1.length - 1; i++) {
                        var arr2 = arr1[i].split('α');
                        var rownum = $("#viewAtt_table tr").length - 1;


                        var value = "<select class='form-control' id='att_" + rownum + "'  value='" + arr2[0] + "'  name='value_" + rownum + "' width='90%'>" + $("#options").val() + "</select>";

                        var order = "<input class='form-control' type='text' id='order_" + rownum + "' value='" + arr2[1] + "' name='order_" + rownum + "' width='50px'/>";
                        var width = "<input class='form-control' type='text' id='width_" + rownum + "' value='" + arr2[2] + "' name='width_" + rownum + "' width='50px'/>";
                        var viewOrderBy = "<input class='form-control' type='text' id='viewOrderBy_" + rownum + "' value='" + arr2[3] + "' name='viewOrderBy_" + rownum + "' width='50px'/>";



                        var button = "<button type='button' class='btn btn-primary ng-binding' onclick='DelRow(" + rownum + ");'>删除</button>";
                        var row = "<tr id='" + rownum + "' ><td>" + value + "</td><td>" + order + "</td><td>" + width + "</td><td>" + viewOrderBy + "</td><td>" + button + "</td></tr>";
                        $(row).insertAfter($("#viewAtt_table tr:eq(" + rownum + ")"));
                    }

                    $("select[id^='att_']").each(function () {
                        $this = $(this);
                        $this.val($this.attr("value"));
                    });

                },
                error: function (data, textStatus) {
                    alert("ajax错误");

                }

            });

        }
        //获取下拉字段
        function getWords() {
            var value = $("#ViewAttProcedure").val();
            if (value == "" || value == null) {
                alert("请填写字段存储过程名！");
            }
            else {
                $.ajax({
                    url: "/Service/Entity/GetViewAttribute.ashx",
                    async: false,
                    type: "post",
                    dataType: "text",
                    data: {
                        "entityId": $("#EntityId").val(),
                        "procedureName": value
                    },
                    success: function (data, textStatus) {
                        $("#options").val(data);
                        var newhtml = "<thead><tr><th>字段</th><th>排序</th>"
                                + "<th>宽度(px)(就写数字就好了，单位不用写)</th><th>视图默认排序(asc|desc)(多个填写的以最下面的为准)</th>"
                                + "<th>操作</th></tr></thead><tbody></tbody>";
                        $("#viewAtt_table").html(newhtml);
                    },
                    error: function (data, textStatus) {
                        alert("ajax错误");

                    }

                });
            }
        }

        //设置Template
        function setTemplete() {
            var se = confirm("View配置修改是否已保存？");
            if (se == true) {
                var infos = "";
                infos += "Id=" + $("#EntityId").val();
                infos += "&entityname=" + $("#EntityName").val();
                infos += "&viewname=" + $("#ViewMapName").val();
                //infos += "&templete=" + encodeURI($("#ViewTemplete").val());
                window.open("AddTemplatePage.aspx?" + infos, "child");
            }
        }

    </script>
    <style>
        .page-header {
            margin: 15px;
        }

            .page-header + .btn-primary {
                margin: 0px 15px 10px 5px;
            }

        .panel-heading {
            padding: 3px 15px;
            height: 40px;
            line-height: 34px;
        }

        .BootStrapSelect {
            background-color: #fff;
            border-bottom-color: #eee;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <input id="EntityId" type="hidden" value="<%=_EntityId %>" />
        <input id="EntityName" type="hidden" value="<%=EntityName %>" />
        <input id="ViewMapName" type="hidden" value="<%=ViewMapName %>" />
        <input id="edit" type="hidden" value="<%=edit %>" />
        <input id="options" type="hidden" value="<%=options %>" />
        <div class="">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header" id="page_title">新建视图</h1>
                    <button type='button' style="float: right" id='Button1' class='btn btn-primary ng-binding'
                        onclick="btn_return();">
                        取消</button>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                            <div class="form-group">
                                <label for="DisplayName">
                                    视图中文名</label>
                                <input type="text" class="form-control" id="DisplayName" />
                            </div>
                            <div class="form-group">
                                <label for="Name">
                                    视图英文名</label>
                                <div class="input-group">
                                    <div class="input-group-addon">
                                        <span id="prefix"><%=Prefix%>_</span>
                                    </div>
                                    <input type="text" class="form-control" id="Name" nomodify="noModify" />

                                </div>
                            </div>
                            <div class="form-group">
                                <label for="ViewClientType">
                                    类型</label>
                                <select class="form-control" id="ViewClientType">
                                    <option value="10" selected>电脑端</option>
                                    <option value="20">移动端</option>
                                </select>
                            </div>
                            <%--<div class="form-group">
                                <label for="ViewIsDisplay">
                                    类型</label>
                                <select class="form-control" id="ClientType">
                                    <option value="1" selected>电脑端</option>
                                    <option value="0">移动端</option>
                                </select>
                            </div>--%>
                            <div class="form-group">
                                <label for="ViewIsDisplay">
                                    显示</label>
                                <select class="form-control" id="ViewIsDisplay">
                                    <option value="1" selected>是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="ViewFilter">
                                    筛选条件</label>
                                <input type="text" class="form-control" id="ViewFilter" />
                            </div>

                            <div class="form-group">
                                <label for="ViewAdditionalJs">
                                    附加JS</label>
                                <!--<input type="text" class="form-control" id="ViewAdditionalJs" />-->
                                <select id="JsList" data-style="BootStrapSelect" data-width="100%" data-live-search="true" multiple></select>
                            </div>

                            <div class="form-group">
                                <label for="ViewTemplete">
                                    Templete</label>
                                <textarea class="form-control" id="ViewTemplete"></textarea>
                            </div>
                            <div class="form-group">
                                <button type="button" class="btn btn-primary ng-binding" onclick="setTemplete();">
                                    配置Templete</button>
                            </div>
                            <div class="form-group">
                                <label for="ViewAttProcedure">
                                    字段存储过程</label>
                                <input type="text" class="form-control" runat="server" id="ViewAttProcedure" />
                            </div>

                            <div class="form-group" id="newAtt_div">
                                <button type="button" class="btn btn-primary ng-binding" onclick="getWords();">
                                    取字段</button>
                                <button type="button" style="margin-left: 20px;" class="btn btn-primary ng-binding" onclick="btnAddRow();">
                                    添加</button>
                                <table class="table table-condensed" id="viewAtt_table">
                                    <thead>
                                        <tr>
                                            <th>字段
                                            </th>

                                            <th>排1序
                                            </th>
                                            <th>宽度(px)(就写数字就好了，单位不用写)
                                            </th>
                                            <th>视图默认排序(asc|desc)(多个填写的以最下面的为准)
                                            </th>
                                            <th>操作
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                            <div class="form-group" id="add_btn_div" style="display: none;">
                                <div class="col-sm-offset-2 col-sm-10">

                                    <button type="button" class="btn btn-primary ng-binding" onclick="addViewMap(1);">
                                        保存并关闭</button>
                                    <button type="button" class="btn btn-primary ng-binding" onclick="addViewMap(2);">
                                        保存</button>

                                </div>
                            </div>
                            <div class="form-group" id="update_btn_div" style="display: none;">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <button type="button" class="btn btn-primary ng-binding" onclick="addViewMap(1);">
                                        保存并关闭</button>
                                    <button type="button" class="btn btn-primary ng-binding" onclick="addViewMap(2);">
                                        保存</button>
                                </div>
                            </div>
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
            </div>
        </div>
    </form>
</body>
</html>
